//
//  CLUIBaseTableViewController.m
//  CityHunter
//
//  Created by chuanqixiong on 14/11/25.
//  Copyright (c) 2014年 CieNet. All rights reserved.
//

#import "CLUIBaseTableViewController.h"
#import "ConfigDefine.h"
#import "Common.h"

 #define kNavigationBtnSpaceHorizontal 5.0f
#define kNavigationBtnSpaceVertical 10.0f
#define kNavigationBtnHeight (self.navigationController.navigationBar.frame.size.height - 2 * kNavigationBtnSpaceVertical)

#define kTitleBarHeight 64.0
#define kBottomBarHeight                    44.0

#define kNavigationBarLeftBtnTag            1000
#define kNavigationBarRightBtnTag            1001
#define kRequestFailRetBtnTag                10000

#ifndef GET_VIEW_RIGHT
#define GET_VIEW_RIGHT(view) (view.frame.origin.x + view.frame.size.width)
#endif
#ifndef GET_VIEW_BOTTOM
#define GET_VIEW_BOTTOM(view) (view.frame.origin.y + view.frame.size.height)
#endif
#define FrameHeight 64
#define Height 44

#define CELLACCESSORYIMG @"pub_title_backbtn_rightarrow"

@interface CLUIBaseTableViewController ()

@property (nonatomic, retain) NSArray *requestList;

@property (nonatomic, retain) UIView *maskView;
@property (nonatomic, retain) __block MBProgressHUD *progressHUD;

 @end

@implementation CLUIBaseTableViewController

/**
 * @brief
 *
 * Detailed comments of this function
 * @param[in]
 * @param[out]
 * @return
 * @note
 */
- (void)viewDidLoad {
    [super viewDidLoad];
      [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor whiteColor], NSForegroundColorAttributeName,
                                                                     //    [UIFont systemFontOfSize:], NSFontAttributeName,
                                                                     nil]];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;

 }

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
 }

/**
 * @brief
 *
 * Detailed comments of this function
 * @param[in]
 * @param[out]
 * @return
 * @note
 */
- (void)viewDidUnload {
    //仅仅在IOS6之前的版本有效
    [super viewDidUnload];
 }

- (void)didReceiveMemoryWarning {
    NSLog(@"%@",[self class]);
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 * @brief
 *
 * Detailed comments of this function
 * @param[in]
 * @param[out]
 * @return
 * @note
 */
- (void)dealloc {
//    [[BusinessFramework defaultBusinessFramework] unregisterBusinessListener:self];
    NSLog(@"[%@ dealloc]",[self class]);

#ifndef COMPILE_ARC_MODE
    [super dealloc];
#endif
}

/**
 * @brief
 *
 * Detailed comments of this function
 * @param[in]
 * @param[out]
 * @return
 * @note
 */
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - navigation

- (void)showNavBarWithRightImage:(UIImage *)rimage
               andRightBtnAction:(SEL)raction
                    LeftBtnImage:(UIImage *)limage
                andLeftBtnAction:(SEL)laction
                           title:(NSString *)title
                        barColor:(UIColor *)color {
    if (limage) {
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setBackgroundImage:limage forState:UIControlStateNormal];
        if (laction) {
            [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
        }
        leftBtn.tag = kNavigationBarLeftBtnTag;
        CGFloat height = kNavigationBtnHeight;
        CGFloat width = height * limage.size.width / limage.size.height;
        leftBtn.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   width,
                                   height);
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    }
    if (rimage) {
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setBackgroundImage:rimage forState:UIControlStateNormal];
        if (raction) {
            [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
        }
        rightBtn.tag = kNavigationBarRightBtnTag;
        CGFloat height = kNavigationBtnHeight;
        CGFloat width = height * rimage.size.width / rimage.size.height;
        rightBtn.frame = CGRectMake(0,
                                    kNavigationBtnSpaceHorizontal,
                                    width,
                                    height);
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    }
    
    if (color) {
        [self.navigationController.navigationBar setBackgroundImage:nil
                                                      forBarMetrics:UIBarMetricsDefault];
        if ([self.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
            self.navigationController.navigationBar.barTintColor = color;
        } else {
            self.navigationController.navigationBar.tintColor = GLOBAL_BARTINT_COLOR;
        }
    }
    [self showNavBarWithTitle:title];
}

- (void)showNavBarWithRightTitle:(NSString *)rightTitle andRightAction:(SEL)raction andLeftAction:(SEL)laction  andFontSize:(CGFloat)size andRightWidth:(CGFloat)width{
    
    //左边返回为箭头，右边是文字
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
    if (laction) {
        [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
        
    }else{
        [leftBtn addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
        
    }
    leftBtn.frame = CGRectMake(0,
                               kNavigationBtnSpaceHorizontal,
                               kNavigationBtnHeight * [UIImage imageNamed:BACKIMAGE].size.width / [UIImage imageNamed:BACKIMAGE].size.height,
                               kNavigationBtnHeight);
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBar;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:CGRectMake(15, kNavigationBtnSpaceHorizontal, width, 30)];
    [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
    [rightBtn.titleLabel setFont: [UIFont systemFontOfSize:size]];
    UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rigthBar;
    
    
}

- (void)showNavBarWithRightTitle:(NSString *)rightTitle andRightAction:(SEL)raction andLeftAction:(SEL)laction  andCustom:(BOOL) bol{
    //左边返回为箭头，右边是文字
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
    if (laction) {
        [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
        
    }else{
        [leftBtn addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    }
 
    leftBtn.frame = CGRectMake(0,
                               kNavigationBtnSpaceHorizontal,
                               kNavigationBtnHeight * [UIImage imageNamed:BACKIMAGE].size.width / [UIImage imageNamed:BACKIMAGE].size.height,
                               kNavigationBtnHeight);
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBar;
    
    if (bol) {
       
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setFrame:CGRectMake(15, kNavigationBtnSpaceHorizontal, 60, 30)];
        [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
        [rightBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        self.navigationItem.rightBarButtonItem = rigthBar;
        
    }
    
}


- (void)showNavBarWithTitle:(NSString *)title {
    if (title) {
        //LogInfo(@"self.navigationController.navigationItem %@", self.navigationController.navigationItem);
        //self.navigationController.navigationItem.title = title;
        self.title = title;
    }
}
 - (void)setNavBarBgImgView:(NSString *)imageStr {
    self.navigationController.navigationBarHidden = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    if ([Common systemVersion] == ESystemVer6OrLess) {
        if (!imageStr) {
            return;
            //imageStr = @"bg_title_ios6";
        }
    } else {
        if (!imageStr) {
            return;
            //imageStr = @"bg_title_ios7";
        }
        self.navigationController.navigationBar.translucent = NO;
    }
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:imageStr]
                                                                 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                  forBarMetrics:UIBarMetricsDefault];
}

- (void)addTableHeaderViewOrFooterView:(BOOL)bol{
    UIView *view = [[UIView alloc] init];
    if (bol) { //头视图
        self.tableView.tableHeaderView = view;
    }else{
        self.tableView.tableFooterView = view;
    }
}

- (void)backTo
{
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)rightBtnAction {
    
}

#pragma mark - table

- (void)setAccessoryView:(UITableViewCell *)cell{
    [cell setAccessoryView: [[UIImageView alloc] initWithImage:[UIImage imageNamed:CELLACCESSORYIMG]]];
}

#pragma mark - mask

- (void)displayMask {
    if (!self.maskView) {
        self.maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        //self.maskView.backgroundColor = [UIColor grayColor];
        //self.maskView.alpha = 0.8f;
        self.maskView.backgroundColor = [UIColor clearColor];
        self.maskView.alpha = 1.f;
    }
    if (!self.maskView.superview) {
        [self.view.window addSubview:self.maskView];
    }
}

- (void)hideMask {
    if (self.maskView) {
        [self.maskView removeFromSuperview];
    }
}
- (void)displayProgressHUD {
    if (!self.progressHUD) {
        [CLProgressHUD showWaitAlertWithView:self.view.window];
    
    }
}

- (void)hideProgressHUD {
    if (self.progressHUD) {
        [CLProgressHUD hideProgressHUDWithView:self.view.window];
        
    }
}

- (void)hideActionSheets {
    for (UIView *view in [[[UIApplication sharedApplication] keyWindow] subviews]) {
        if ([view isKindOfClass:[JGActionSheet class]]) {
            JGActionSheet *sheet = (JGActionSheet*)view;
            [sheet dismissAnimated:YES];
        }
    }
}
#pragma mark - network reuqest

- (void)registerRequestList:(NSArray *)arr{
    self.requestList = [arr copy];
}

#pragma mark - 网络请求代理
-(void)networkingStartHandleWithRequestIdentifier:(RequestIdentifier)identifier
{
    [self.view makeToastActivity];//现实加载中
    
    
}

-(void)networkingCompleteHandleWithRequestIdentifier:(RequestIdentifier)identifier
{
    [self.view hideToastActivity];//隐藏加载中
    
}

-(void)networkingFailureHandleWithRequestIdentifier:(RequestIdentifier)identifier errorMessage:(NSString *)message
{
    [self.view makeToast:message];
    
}




- (void)requestFailOption:(NSError*)error {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[Common getLocalStr:@"NetworkRequestFail"]
                                                    message:[error.userInfo objectForKey:@"errorMsg" ]
                                                   delegate:self
                                          cancelButtonTitle:[Common getLocalStr:@"CommonTitleConfirm"]
                                          otherButtonTitles:nil, nil];
    alert.tag = kRequestFailRetBtnTag;
    [alert show];
}

#pragma mark - Present

- (BOOL)isPresent {
    if (self.navigationController) {
        return self == self.navigationController.viewControllers.lastObject;
    }
    return YES;
}

//
//- (UIViewController*)popToViewControllerKindClass:(id)viewControllerClass animated:(BOOL)animated {
//    if (!viewControllerClass) {
//        return nil;
//    }
//    
//    NSArray *arr;
//    if (![viewControllerClass isKindOfClass:[NSArray class]]) {
//        arr = [NSArray arrayWithObject:viewControllerClass];
//    } else {
//        arr = viewControllerClass;
//    }
//    
//    for (UIViewController *viewController in [self.navigationController.viewControllers reverseObjectEnumerator]) {
//        for (Class class in arr) {
//            if ([viewController isKindOfClass:class]) {
//                [self popToViewController:viewController animated:animated];
//                return viewController;
//            }
//        }
//    }
//    
//    return nil;
//}
//
//- (UIViewController*)popToViewControllerMemberClass:(id)viewControllerClass animated:(BOOL)animated {
//    if (!viewControllerClass) {
//        return nil;
//    }
//    
//    NSArray *arr;
//    if (![viewControllerClass isKindOfClass:[NSArray class]]) {
//        arr = [NSArray arrayWithObject:viewControllerClass];
//    } else {
//        arr = viewControllerClass;
//    }
//    
//    for (UIViewController *viewController in [self.navigationController.viewControllers reverseObjectEnumerator]) {
//        for (Class class in arr) {
//            if ([viewController isKindOfClass:class]) {
//                [self popToViewController:viewController animated:animated];
//                return viewController;
//            }
//        }
//    }
//    
//    return nil;
//}

#pragma mark - 重新登录
//
//- (void)retryLogin {
//    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//    StartViewController *start = [story instantiateViewControllerWithIdentifier:@"Start"];
//    start.startMode = StartModeRetry;
//    [self.navigationController pushViewController:start animated:NO];
//    [start performSegueWithIdentifier:@"startCustomPushLogin" sender:start];
//}

@end
